const puppeteer = require('puppeteer')
const config = require('./config')

async function start() {

    const browser = await puppeteer.launch(
        {
            headless: false,
            // executablePath: 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe',
            executablePath: config.chromiumPath,
            defaultViewport: {
                width: 1600,
                height: 800
            }
        }
    );

    const page = await browser.newPage();

    const url = 'http://192.168.5.118:30017/'
    await page.goto(`${url}#/login`, {
        // waitUntil: ['domcontentloaded']
    })

    await page.evaluate(() => {
        let date = Date.now() + 999999999
        // let accessToken = '52acf89f5f65093bdeb47774568cc3e5'
        let accessToken = 'b5434f9e18a3769253e32482ef0525f5'
        localStorage.setItem('cecdata_uac_user', JSON.stringify({
            accessExpire: date,
            accessToken: accessToken,
            expirationTime: date,
            loginExpire: date,
            loginToken: "123",
        }));
    });
    await page.goto(url)

    await page.waitForSelector('.ant-menu')

    await page.click('.bui-frame-sider-collapsed-right')

    await page.waitFor(1000);

    let menus = await page.$$('.ant-menu li.ant-menu-submenu')

    let index = 0
    for(let menu of menus) {
        menu.click()
        await page.waitFor(200);
        let subMenus = await menu.$$('.ant-menu-item .bui-common-menu-li-title')
        for (let subMenu of subMenus) {
            if(index > 6) continue
            let title = await subMenu.evaluate(node => {
                node.click()
                return node.innerText
            })
            await page.waitFor(3000)
            await page.screenshot({
                path: `./screenshot/${title}.png`
            });
            index++
        }
    }

    await browser.close();
}

start(2316993)